<?php
include_once 'root.php';
Class AdminPanel extends Root {
	public function __construct() {
		parent::__construct();
		
		$configSap = array(
		"logindata"=>array(
			"ASHOST"=>"/H/101.109.242.24/H/191.8.209.246"	// application server
			,"SYSNR"=>"02"					// system number
			,"CLIENT"=>"300"					// client
			,"USER"=>"vasawat"				// user
			,"PASSWD"=>"kohkae"				// password
			,"LANG"=>"TH"					// language
			,"CODEPAGE"=>"8600"				// codepage 8600 for support thai language
			/*"ASHOST"=>"192.168.19.215"	// application server
			,"SYSNR"=>"00"					// system number
			,"CLIENT"=>"330"					// client
			,"USER"=>"winitap"				// user
			,"PASSWD"=>"alex1100"				// password
			,"LANG"=>"TH"					// language
			,"CODEPAGE"=>"8600"				// codepage 8600 for support thai language*/
		)
		,"show_errors"=>false					// let class printout errors
		,"debug"=>false) ;					// detailed debugging information

		$this -> load -> library("saprfc",$configSap);
		
	}

	public function index() {
		$this -> data['header'] = "admin panel";
		$this -> includeView("adminpanel");
	}
	
	public function importlog() {
		$this->data['header'] = "import logs";
		$this->pagination->create_links();
		$this->load->model("importlogmodel");
		$q  = $this->getCoreQuery();
		$allRow = $this->importlogmodel->getImportCount($q);
		$pageConfig = $this->initPagination($allRow);
		$this->data['pagination'] = $this->pagination;
		$this->data['dataList'] = $this->importlogmodel->getImport($q,$pageConfig['start'],$pageConfig['limit']);
		$this->includeView("viewimportlog");
	}
	
	public function exportlog(){
		$this->data['header'] = "export logs";
		$this->includeView("wait");
	}
	
	public function sapmanual(){
		$this->data['header'] = "import & export";
		$this->includeView("viewsapmanual");
	}
	
	public function importcustomer(){
		$this->data['header'] = "import customer_data";
		$this -> load -> model("importcustomermodel");
		$result = $this -> saprfc -> callFunction("ZSDRFC_CUSTOMER_DATA",
								array(	array("TABLE","CUSTOMER_DATA",array()),
										array("TABLE","SALES_DATA",array())
								));
		// if start is ok will import data
		if ($this -> saprfc -> getStatus() == SAPRFC_OK) {
			// start import data send $result to function 
			$insert = $this -> importcustomermodel -> insertConnectData($result);
			//print_r $result;
			//exit();			
			// if saprfc OK show message in view page
			//$message = "SAP LOGIN OK";
			//$this -> set_res(true, $message);
			$this->saprfc->logoff();
			if ($insert['flag']) {
				$connectRow  = number_format($insert['connect_row']);
				$message = date("d-m-Y H:i:s")."<br />";
				$message.="TABLE: CUSTOMER_DATA added $connectRow RECORDS";
				$this->data['headerMessage'] = "insert complete";
				$this->data['message']  = $message;
				//$this -> set_res(true, $message);
			} else {
				$message = date("d-m-Y H:i:s")."<br />";
				$message.="TABLE: CUSTOMER_DATA added 0 RECORDS";
				$this->data['headerMessage'] = "insert failed";
				$this->data['message']  = $message;
			}
		} else {
			// if not conect sap
			$this->load->model("coremodel");
			// write log for can connect model
			$this->coremodel->insertLogFailed('CUSTOMER_DATA', 'adminpanel/importcustomer');
			$resultMesage = $this -> saprfc -> getStatusText();
			//$resultMesage = "";
			$this->saprfc->logoff();
			
			$message = date("d-m-Y H:i:s")."<br />";
			$message.="ERROR ON Connect SAP <br />TABLE: CUSTOMER_DATA added 0 RECORDS";
			$message.='<br /> with message '.$resultMesage;
			$this->data['headerMessage'] = "Error On Connection";
			$this->data['message']  = $message;

		}
		$this->includeView("resultimport");
	}
	
	public function importsales(){
		$this->data['header'] = "import sales_data";
		$this -> load -> model("importsalesmodel");
		$result = $this -> saprfc -> callFunction("ZSDRFC_CUSTOMER_DATA",
								array(	array("TABLE","CUSTOMER_DATA",array()),
										array("TABLE","SALES_DATA",array())
								));
		// if start is ok will import data
		if ($this -> saprfc -> getStatus() == SAPRFC_OK) {
			// start import data send $result to function 
			$insert = $this -> importsalesmodel -> insertConnectData($result);
			$this->saprfc->logoff();
			if ($insert['flag']) {
				$connectRow  = number_format($insert['connect_row']);
				$message = date("d-m-Y H:i:s")."<br />";
				$message.="TABLE: SALES_DATA added $connectRow RECORDS";
				$this->data['headerMessage'] = "insert complete";
				$this->data['message']  = $message;
				//$this -> set_res(true, $message);
			} else {
				$message = date("d-m-Y H:i:s")."<br />";
				$message.="TABLE: SALES_DATA added 0 RECORDS";
				$this->data['headerMessage'] = "insert failed";
				$this->data['message']  = $message;
			}
		} else {
			// if not conect sap
			$this->load->model("coremodel");
			// write log for can connect model
			$this->coremodel->insertLogFailed('SALES_DATA', 'adminpanel/importsales');
			$resultMesage = $this -> saprfc -> getStatusText();
			//$resultMesage = "";
			$this->saprfc->logoff();
			
			$message = date("d-m-Y H:i:s")."<br />";
			$message.="ERROR ON Connect SAP <br />TABLE: SALES_DATA added 0 RECORDS";
			$message.='<br /> with message '.$resultMesage;
			$this->data['headerMessage'] = "Error On Connection";
			$this->data['message']  = $message;

		}
		$this->includeView("resultimport");
	}
	
	public function importmaterial(){
		$this->data['header'] = "import material_data";
		$this -> load -> model("importmaterialmodel");
		$result = $this -> saprfc -> callFunction("ZSDRFC_MATERIAL_DATA",
								array(	array("TABLE","MATERIAL_DATA",array())
								));
		// if start is ok will import data
		if ($this -> saprfc -> getStatus() == SAPRFC_OK) {
			// start import data send $result to function 
			$insert = $this -> importmaterialmodel -> insertConnectData($result);
			$this->saprfc->logoff();
			if ($insert['flag']) {
				$connectRow  = number_format($insert['connect_row']);
				$message = date("d-m-Y H:i:s")."<br />";
				$message.="TABLE: MATERIAL_DATA added $connectRow RECORDS";
				$this->data['headerMessage'] = "insert complete";
				$this->data['message']  = $message;
				//$this -> set_res(true, $message);
			} else {
				$message = date("d-m-Y H:i:s")."<br />";
				$message.="TABLE: MATERIAL_DATA added 0 RECORDS";
				$this->data['headerMessage'] = "insert failed";
				$this->data['message']  = $message;
			}
		} else {
			// if not conect sap
			$this->load->model("coremodel");
			// write log for can connect model
			$this->coremodel->insertLogFailed('MATERIAL_DATA', 'adminpanel/importmaterial');
			$resultMesage = $this -> saprfc -> getStatusText();
			//$resultMesage = "";
			$this->saprfc->logoff();
			
			$message = date("d-m-Y H:i:s")."<br />";
			$message.="ERROR ON Connect SAP <br />TABLE: MATERIAL_DATA added 0 RECORDS";
			$message.='<br /> with message '.$resultMesage;
			$this->data['headerMessage'] = "Error On Connection";
			$this->data['message']  = $message;

		}
		$this->includeView("resultimport");
	}
	
	public function importpromotion(){
		$this->data['header'] = "import promotion";
		$this -> load -> model("importpromotionmodel");
		$result = $this -> saprfc -> callFunction("ZSDRFC_SALESPROMO_DATA",
								array(	array("TABLE","PROMOTION",array()),
											array("TABLE","PROMOTION_SMAT",array()),
											array("TABLE","PROMOTION_FMAT",array()),
										    array("TABLE","PROMOTION_LEVEL",array())
								));
		// if start is ok will import data
		if ($this -> saprfc -> getStatus() == SAPRFC_OK) {
			// start import data send $result to function 
			$insert = $this -> importpromotionmodel -> insertConnectData($result);
			$this->saprfc->logoff();
			if ($insert['flag']) {
				$connectRow  = number_format($insert['connect_row']);
				$message = date("d-m-Y H:i:s")."<br />";
				$message.="TABLE: PROMOTION added $connectRow RECORDS";
				$this->data['headerMessage'] = "insert complete";
				$this->data['message']  = $message;
				//$this -> set_res(true, $message);
			} else {
				$message = date("d-m-Y H:i:s")."<br />";
				$message.="TABLE: PROMOTION added 0 RECORDS";
				$this->data['headerMessage'] = "insert failed";
				$this->data['message']  = $message;
			}
		} else {
			// if not conect sap
			$this->load->model("coremodel");
			// write log for can connect model
			$this->coremodel->insertLogFailed('PROMOTION', 'adminpanel/importpromotion');
			$resultMesage = $this -> saprfc -> getStatusText();
			//$resultMesage = "";
			$this->saprfc->logoff();
			
			$message = date("d-m-Y H:i:s")."<br />";
			$message.="ERROR ON Connect SAP <br />TABLE: PROMOTION added 0 RECORDS";
			$message.='<br /> with message '.$resultMesage;
			$this->data['headerMessage'] = "Error On Connection";
			$this->data['message']  = $message;

		}
		$this->includeView("resultimport");
	}
	
	public function importpromotionfmat(){
		$this->data['header'] = "import promotion_fmat";
		$this -> load -> model("importpromotionfmatmodel");
		$result = $this -> saprfc -> callFunction("ZSDRFC_SALESPROMO_DATA",
								array(	array("TABLE","PROMOTION",array()),
											array("TABLE","PROMOTION_SMAT",array()),
											array("TABLE","PROMOTION_FMAT",array()),
										    array("TABLE","PROMOTION_LEVEL",array())
								));
		// if start is ok will import data
		if ($this -> saprfc -> getStatus() == SAPRFC_OK) {
			// start import data send $result to function 
			$insert = $this -> importpromotionfmatmodel -> insertConnectData($result);
			$this->saprfc->logoff();
			if ($insert['flag']) {
				$connectRow  = number_format($insert['connect_row']);
				$message = date("d-m-Y H:i:s")."<br />";
				$message.="TABLE: PROMOTION_FMAT added $connectRow RECORDS";
				$this->data['headerMessage'] = "insert complete";
				$this->data['message']  = $message;
				//$this -> set_res(true, $message);
			} else {
				$message = date("d-m-Y H:i:s")."<br />";
				$message.="TABLE: PROMOTION_FMAT added 0 RECORDS";
				$this->data['headerMessage'] = "insert failed";
				$this->data['message']  = $message;
			}
		} else {
			// if not conect sap
			$this->load->model("coremodel");
			// write log for can connect model
			$this->coremodel->insertLogFailed('PROMOTION_FMAT', 'adminpanel/importpromotionfmat');
			$resultMesage = $this -> saprfc -> getStatusText();
			//$resultMesage = "";
			$this->saprfc->logoff();
			
			$message = date("d-m-Y H:i:s")."<br />";
			$message.="ERROR ON Connect SAP <br />TABLE: PROMOTION_FMAT added 0 RECORDS";
			$message.='<br /> with message '.$resultMesage;
			$this->data['headerMessage'] = "Error On Connection";
			$this->data['message']  = $message;

		}
		$this->includeView("resultimport");
	}
	
	public function importpromotionsmat(){
		$this->data['header'] = "import promotion_smat";
		$this -> load -> model("importpromotionsmatmodel");
		$result = $this -> saprfc -> callFunction("ZSDRFC_SALESPROMO_DATA",
								array(	array("TABLE","PROMOTION",array()),
											array("TABLE","PROMOTION_SMAT",array()),
											array("TABLE","PROMOTION_FMAT",array()),
										    array("TABLE","PROMOTION_LEVEL",array())
								));
		// if start is ok will import data
		if ($this -> saprfc -> getStatus() == SAPRFC_OK) {
			// start import data send $result to function 
			$insert = $this -> importpromotionsmatmodel -> insertConnectData($result);
			$this->saprfc->logoff();
			if ($insert['flag']) {
				$connectRow  = number_format($insert['connect_row']);
				$message = date("d-m-Y H:i:s")."<br />";
				$message.="TABLE: PROMOTION_SMAT added $connectRow RECORDS";
				$this->data['headerMessage'] = "insert complete";
				$this->data['message']  = $message;
				//$this -> set_res(true, $message);
			} else {
				$message = date("d-m-Y H:i:s")."<br />";
				$message.="TABLE: PROMOTION_SMAT added 0 RECORDS";
				$this->data['headerMessage'] = "insert failed";
				$this->data['message']  = $message;
			}
		} else {
			// if not conect sap
			$this->load->model("coremodel");
			// write log for can connect model
			$this->coremodel->insertLogFailed('PROMOTION_SMAT', 'adminpanel/importpromotionsmat');
			$resultMesage = $this -> saprfc -> getStatusText();
			//$resultMesage = "";
			$this->saprfc->logoff();
			
			$message = date("d-m-Y H:i:s")."<br />";
			$message.="ERROR ON Connect SAP <br />TABLE: PROMOTION_SMAT added 0 RECORDS";
			$message.='<br /> with message '.$resultMesage;
			$this->data['headerMessage'] = "Error On Connection";
			$this->data['message']  = $message;

		}
		$this->includeView("resultimport");
	}
	
	public function importpromotionlevel(){
		$this->data['header'] = "import promotion_level";
		$this -> load -> model("importpromotionlevelmodel");
		$result = $this -> saprfc -> callFunction("ZSDRFC_SALESPROMO_DATA",
								array(	array("TABLE","PROMOTION",array()),
											array("TABLE","PROMOTION_SMAT",array()),
											array("TABLE","PROMOTION_FMAT",array()),
										    array("TABLE","PROMOTION_LEVEL",array())
								));
		// if start is ok will import data
		if ($this -> saprfc -> getStatus() == SAPRFC_OK) {
			// start import data send $result to function 
			$insert = $this -> importpromotionlevelmodel -> insertConnectData($result);
			$this->saprfc->logoff();
			if ($insert['flag']) {
				$connectRow  = number_format($insert['connect_row']);
				$message = date("d-m-Y H:i:s")."<br />";
				$message.="TABLE: PROMOTION_LEVEL added $connectRow RECORDS";
				$this->data['headerMessage'] = "insert complete";
				$this->data['message']  = $message;
				//$this -> set_res(true, $message);
			} else {
				$message = date("d-m-Y H:i:s")."<br />";
				$message.="TABLE: PROMOTION_LEVEL added 0 RECORDS";
				$this->data['headerMessage'] = "insert failed";
				$this->data['message']  = $message;
			}
		} else {
			// if not conect sap
			$this->load->model("coremodel");
			// write log for can connect model
			$this->coremodel->insertLogFailed('PROMOTION_LEVEL', 'adminpanel/importpromotionlevel');
			$resultMesage = $this -> saprfc -> getStatusText();
			//$resultMesage = "";
			$this->saprfc->logoff();
			
			$message = date("d-m-Y H:i:s")."<br />";
			$message.="ERROR ON Connect SAP <br />TABLE: PROMOTION_LEVEL added 0 RECORDS";
			$message.='<br /> with message '.$resultMesage;
			$this->data['headerMessage'] = "Error On Connection";
			$this->data['message']  = $message;

		}
		$this->includeView("resultimport");
	}
	
	public function importsalesreport(){
		$this->data['header'] = "import salesrevenue";
		$this -> load -> model("importsalesreportmodel");
		$result = $this -> saprfc -> callFunction("ZSDRFC_SALESREVENUE",
								array(	array("TABLE","IT_DATA",array())
								));
		// if start is ok will import data
		if ($this -> saprfc -> getStatus() == SAPRFC_OK) {
			// start import data send $result to function 
			$insert = $this -> importsalesreportmodel -> insertConnectData($result);
			$this->saprfc->logoff();
			if ($insert['flag']) {
				$connectRow  = number_format($insert['connect_row']);
				$message = date("d-m-Y H:i:s")."<br />";
				$message.="TABLE: IT_DATA added $connectRow RECORDS";
				$this->data['headerMessage'] = "insert complete";
				$this->data['message']  = $message;
				//$this -> set_res(true, $message);
			} else {
				$message = date("d-m-Y H:i:s")."<br />";
				$message.="TABLE: IT_DATA added 0 RECORDS";
				$this->data['headerMessage'] = "insert failed";
				$this->data['message']  = $message;
			}
		} else {
			// if not conect sap
			$this->load->model("coremodel");
			// write log for can connect model
			$this->coremodel->insertLogFailed('IT_DATA', 'adminpanel/importsalesreport');
			$resultMesage = $this -> saprfc -> getStatusText();
			//$resultMesage = "";
			$this->saprfc->logoff();
			
			$message = date("d-m-Y H:i:s")."<br />";
			$message.="ERROR ON Connect SAP <br />TABLE: IT_DATA added 0 RECORDS";
			$message.='<br /> with message '.$resultMesage;
			$this->data['headerMessage'] = "Error On Connection";
			$this->data['message']  = $message;

		}
		$this->includeView("resultimport");
	}

	public function exportsaleorder(){
		$this->data['header'] = "export sales order";
		$this -> load -> model("exportsaleordermodel");
		
		$this->load->model("exportsaleordermodel");
    	$data = $this->exportsaleordermodel->getAll(); 
		//$this -> data['dataList'] = $this -> exportsaleordermodel->getAll();
		//$this->includeView("viewexportdata");
		
		$result = $this -> saprfc -> callFunction("ZSDRFC_BAPI_SALESORDER",
								array(	array("IMPORT","CALL_BAPI","X"),
										  //array("TABLE","WEB_DATA",array())
										  array("TABLE","WEB_DATA",$data)
								));
		// if start is ok will import data
		if ($this -> saprfc -> getStatus() == SAPRFC_OK) {
			// start import data send $result to function 
			$insert = $this -> exportsaleordermodel -> updateConnectData($result);
			//print_r $result;
			//exit();			
			// if saprfc OK show message in view page
			//$message = "SAP LOGIN OK";
			//$this -> set_res(true, $message);
			$this->saprfc->logoff();
			if ($insert['flag']) {
				$connectRow  = number_format($insert['connect_row']);
				$message = date("d-m-Y H:i:s")."<br />";
				$message.="TABLE: ZSD_WEB_ORDER updated $connectRow RECORDS";
				$this->data['headerMessage'] = "update complete";
				$this->data['message']  = $message;
				//$this -> set_res(true, $message);
			} else {
				$message = date("d-m-Y H:i:s")."<br />";
				$message.="TABLE: ZSD_WEB_ORDER added 0 RECORDS";
				$this->data['headerMessage'] = "update failed";
				$this->data['message']  = $message;
			}
		} else {
			// if not conect sap
			$this->load->model("coremodel");
			// write log for can connect model
			$this->coremodel->insertLogFailed('ZSDRFC_BAPI_SALESORDER', 'ZSD_WEB_ORDER', 'adminpanel/exportsaleorder');
			$resultMesage = $this -> saprfc -> getStatusText();
			//$resultMesage = "";
			$this->saprfc->logoff();
			
			$message = date("d-m-Y H:i:s")."<br />";
			$message.="ERROR ON Connect SAP <br />TABLE: ZSD_WEB_ORDER updated 0 RECORDS";
			$message.='<br /> with message '.$resultMesage;
			$this->data['headerMessage'] = "Error On Connection";
			$this->data['message']  = $message;

		}
		
		//$this->data['headerMessage'] = "Data";
		//$this->data['message']  = $data;
		$this->includeView("resultimport");
	}
	
	public function updatesaleorder(){
		$this->data['header'] = "Download Sales Order and status";
		$this -> load -> model("updatesaleordermodel");
		
		//$this->load->model("updatesaleordermodel");
    	//$data = $this->updatesaleordermodel->getAll(); 

		$icreatedate = date("Ymd");
		$result = $this -> saprfc -> callFunction("ZSDRFC_SALESORDER_DATA",
								array(	//array("IMPORT","I_CREATE_DATE","17.07.2015")
										  array("IMPORT","I_CREATE_DATE",$icreatedate),
										  array("TABLE","WEB_DATA",array())
										  //array("TABLE","WEB_DATA",$data)
								));
		// if start is ok will import data
		if ($this -> saprfc -> getStatus() == SAPRFC_OK) {
			// start import data send $result to function 
			$insert = $this -> updatesaleordermodel -> updateConnectData($result);
			//print_r $result;
			//exit();			
			// if saprfc OK show message in view page
			//$message = "SAP LOGIN OK";
			//$this -> set_res(true, $message);
			$this->saprfc->logoff();
			if ($insert['flag']) {
				$connectRow  = number_format($insert['connect_row']);
				$message = date("d-m-Y H:i:s")."<br />";
				$message.="TABLE: ZSD_WEB_ORDER updated $connectRow RECORDS";
				$this->data['headerMessage'] = "update complete";
				$this->data['message']  = $message;
				//$this -> set_res(true, $message);
			} else {
				$message = date("d-m-Y H:i:s")."<br />";
				$message.="TABLE: ZSD_WEB_ORDER added 0 RECORDS";
				$this->data['headerMessage'] = "update failed";
				$this->data['message']  = $message;
			}
		} else {
			// if not conect sap
			$this->load->model("coremodel");
			// write log for can connect model
			$this->coremodel->insertLogFailed('ZSDRFC_SALESORDER_DATA', 'ZSD_WEB_ORDER', 'adminpanel/exportsaleorder');
			$resultMesage = $this -> saprfc -> getStatusText();
			$this->saprfc->logoff();
			
			$message = date("d-m-Y H:i:s")."<br />";
			$message.="ERROR ON Connect SAP <br />TABLE: ZSD_WEB_ORDER updated 0 RECORDS";
			$message.='<br /> with message '.$resultMesage;
			$this->data['headerMessage'] = "Error On Connection";
			$this->data['message']  = $message;

		}
		
		//$this->data['headerMessage'] = "Data";
		//$this->data['message']  = $data;
		$this->includeView("resultimport");
	}

	public function customer() {
		$this->data['header'] = "customer data";
		$this->pagination->create_links();
		$this->load->model("customerdatamodel");
		$q  = $this->getCoreQuery();
		$allRow = $this->customerdatamodel->getImportCount($q);
		$pageConfig = $this->initPagination($allRow);
		$this->data['pagination'] = $this->pagination;
		$this->data['dataList'] = $this->customerdatamodel->getImport($q,$pageConfig['start'],$pageConfig['limit']);
		$this -> includeView("viewcustomerdata");
	}
	public function user(){
		$this->manageuser();
	}
	public function newuser(){
		$this -> checkAuthen("user","create");
		$this -> load -> library('genhtml');
		$this -> data['genhtml'] = $this -> genhtml;
		$this -> genhtml -> setData(array());
		$this->data['header'] = "Craete New user";
		$this->includeView("newuser");
	}
	public function edit($userid){
		$this -> checkAuthen("user","edit");
		$this->load->model("usermodel");
		$data = $this->usermodel->getAUser($userid);
		
		$this -> load -> library('genhtml');
		$this -> data['genhtml'] = $this -> genhtml;
		$this -> genhtml -> setData($data);
		$this->data['header'] = "Edit User ";
		$this->includeView("editu");
	}
	public function manageuser(){
		$this -> checkAuthen("user","view");
		$this -> data['canSearch'] = $this -> canUseFunction("user","search");
		$this -> data['canEdit'] = $this -> canUseFunction("user","edit");
		$q = $this -> getCoreQuery($this -> data['canSearch']);
		
		$allRow = $this -> usermodel -> getUserCount($q);
		$pageConfig = $this -> initPagination($allRow); ;
		$this -> data['pagination'] = $this -> pagination;
		$this->data['header'] = "User List";
		$this->data['userList'] = $this->usermodel->getUser($q,$pageConfig['start'], $pageConfig['limit']);
		$this->includeView("viewuser");
	}
	public function newdatacustomer(){
		$data = $this->Post2Array($_POST);
		foreach($data as $in){
			if($in==""){
				$this->set_res("false","กรุณากรอกข้อมูลให้ครบทุกช่อง");
			}
		}
		if($data['Password']!=$data['Repassword']){
			$this->set_res("false","รหัสผ่านทั้งสองช่องไม่เท่ากัน");
		}
		unset($data['Repassword']);
		
		$this->load->model("usermodel");
		$check = $this->usermodel->checkUser($data);
		if($check['flag']==false){
			$this->set_res("false",$check['message']);
		}
		
		$this->usermodel->createUser($data);
		$this->set_res("true","");
	}
	public function updateDataCustomer(){
		$data = $this->Post2Array($_POST);
		$idUpdate = $data['OldUserID'];
		$rawData = $data;
		
		
		unset($rawData['Password']);
		unset($rawData['Repassword']);
		unset($rawData['OldUserID']);
		
		foreach($rawData as $in){
			if($in==""){
				$this->set_res("false","กรุณากรอกข้อมูลให้ครบทุกช่อง");
			}
		}
		if($data['Password']!=""){
			if($data['Password']!=$data['Repassword']){
				$this->set_res("false","รหัสผ่านทั้งสองช่องไม่เท่ากัน");
			}else{
				$rawData['Password'] = $data['Password'];
			}
		}
		unset($data['Repassword']);
		
		$this->load->model("usermodel");
		$check = $this->usermodel->checkUser($rawData,$idUpdate);
		if($check['flag']==false){
			$this->set_res("false",$check['message']);
		}
		
		$this->usermodel->updateUser($rawData,$idUpdate);
		$this->set_res("true","");
	}
	public function manageauthen($userid){
		
		$this->data['header'] = "Set Authen Of User";
		
		
		
		$this->load->library('genhtml');
		$this->data['genhtml']  = $this->genhtml;
		$this->load->model("usermodel");
		$this->data['userData'] = $this->usermodel->getAUser($userid);
		$this->data['userID'] = $userid;
		$this->data['role'] = $this->usermodel->getRole();
		$this->data['authen'] = $this->usermodel->getAuthen();
		$this->data['myAuthen'] = $this->usermodel->getMyAuthen($userid);
		$this->includeView("editauthen");
	}
	public function updateAuthen(){
		$data = $this->Post2Array($_POST);
		$userID = $data['userID'];
		$array = array();
		foreach($data['autheid'] as $in){
				
			$inData = array();
			$ar = explode(",", $in);
			list($authenID,$roleID,$authenName) = $ar;
			$inData['UserID'] = $userID;
			$inData['AuthenID'] = $authenID;
			$inData['RoleID'] = $roleID;
			$inData['AuthenRoleName'] = $authenName;
			$array[]  = $inData;
		}
		$this->load->model("usermodel");
		$this->usermodel->craeteAuthen($array,$userID);
		$this->set_res("true","อัพเดทข้อมูลเรียบร้อยแล้ว");
	}
}
?>